package smartclass.shape;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.View;

import com.samsung.android.sdk.pen.engine.SpenSurfaceView;

public class CustomDrawableView extends View {

	// private ShapeDrawable mDrawable;
	Paint paint;
	Point point;
	float[] points;
	public static Canvas mCanvas;
	SpenSurfaceView mSpenSurfaceView;

	public CustomDrawableView(Context context, SpenSurfaceView mSpenSurfaceView) {
		super(context);
		this.mSpenSurfaceView = mSpenSurfaceView;
		paint = new Paint();
		paint.setStrokeWidth(4);
		paint.setColor(Color.BLACK);
	}

	public void initGrid() {

		int x = (int) ((mSpenSurfaceView.getCanvasWidth()) / 40);
		int y = (int) (mSpenSurfaceView.getCanvasHeight() / 40);
		if (x * 40 == mSpenSurfaceView.getCanvasWidth())
			x++;
		if (y * 40 == mSpenSurfaceView.getCanvasHeight())
			y++;

		points = new float[x * y * 2];
		int s = 0;
		for (int i = 0; i < x; i++) {
			for (int j = 0; j < y; j++) {
				points[s] = i * 40;
				s++;
				points[s] = j * 40;
				s++;
			}
		}
	}

	public void draw() {
		mCanvas.drawColor(Color.TRANSPARENT,
				android.graphics.PorterDuff.Mode.CLEAR);
		float panX = mSpenSurfaceView.getPan().x;
		float panY = mSpenSurfaceView.getPan().y;

		float x;
		float y;
		float ratio = mSpenSurfaceView.getZoomRatio();

		paint.setStrokeWidth(4 * ratio);

		for (int i = 0; i < points.length; i = i + 2) {
			x = (points[i] - panX) * ratio
					+ mSpenSurfaceView.getFrameStartPosition().x;
			y = (points[i + 1] - panY) * ratio
					+ mSpenSurfaceView.getFrameStartPosition().y;
			mCanvas.drawPoint(x, y, paint);

		}
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// mDrawable.draw(canvas);
		mCanvas = canvas;
		draw();
		
		

	}

	public void drawRect() {
		mCanvas.drawRect(30, 30, 80, 80, paint);
	}
}
